package zxing.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fenshi.bean.CardBean;
import com.fenshi.manager.R;
import com.fenshi.presenter.ScanPresenter;
import com.fenshi.presenter.imp.ScanPresenterImp;
import com.fenshi.view.ScanView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.AlertHelper;

import butterknife.InjectView;

public class ResultActivity extends BaseActivity implements ScanView {

    @InjectView(R.id.erweima)
    ImageView erweima;
    @InjectView(R.id.line)
    TextView line;
    @InjectView(R.id.card_username)
    TextView cardUsername;
    @InjectView(R.id.user_layout)
    LinearLayout userLayout;
    @InjectView(R.id.card_name)
    TextView cardName;
    @InjectView(R.id.code_layout)
    LinearLayout codeLayout;
    @InjectView(R.id.card_money)
    TextView cardMoney;
    @InjectView(R.id.money_layout)
    LinearLayout moneyLayout;
    @InjectView(R.id.card_create_time)
    TextView cardCreateTime;
    @InjectView(R.id.create_time_layout)
    LinearLayout createTimeLayout;
    @InjectView(R.id.card_button)
    Button cardButton;
    private ScanPresenter presenter;
    private ProgressDialog loading;

    @Override
    public int getLayoutID() {
        return R.layout.activity_result;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initActionbar() {

    }

    @Override
    public void initData() {
        AlertHelper helper = new AlertHelper(this);
        loading = helper.LoadingAlert();

        presenter = new ScanPresenterImp(this);
        Intent intent = getIntent();
        String code = intent.getExtras().getString("result");
        presenter.checkCard(code);
    }

    @Override
    public void showLoad() {
        if (loading != null && !loading.isShowing()) loading.show();
    }

    @Override
    public void dismissLoad() {
        if (loading != null && loading.isShowing()) loading.dismiss();
//		onResume();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showRentAlert(CardBean bean) {
        initCard(bean, true);
    }

    @Override
    public void showBackAlert(CardBean bean) {
        initCard(bean, false);
    }

    @Override
    public void showNotAccessAlert(CardBean bean) {
        initCard(bean, null);
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("卡片未审核");
        alert.setMessage("");
        alert.setPositiveButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dif, int i) {

            }
        });
    }

    @Override
    public void checkFaile() {
        Toast.makeText(this, "卡片不可用", Toast.LENGTH_LONG).show();
    }

    public void initCard(CardBean bean, Boolean rend) {
        cardUsername.setText(bean.getUserName());
        cardMoney.setText(bean.getMoney() + "");
//        cardCreateTime.setText(bean.getCreateTime());
        cardName.setText(bean.getCardCode());

        if (rend == null) {
            cardCreateTime.setText("卡片未审核");
            cardCreateTime.setTextColor(getResources().getColor(R.color.red));
            cardButton.setVisibility(View.GONE);
        } else {
            if (rend) {
                cardCreateTime.setText("未使用");
                cardCreateTime.setTextColor(getResources().getColor(R.color.primary));
                cardButton.setText("确认租车");
                cardButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        presenter.isRent();
                    }
                });
            } else {
                cardCreateTime.setText("使用中");
                cardCreateTime.setTextColor(getResources().getColor(R.color.red));
                cardButton.setText("确认锁车");
                cardButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        presenter.isBack(cardName.getText().toString());
                    }
                });
            }
        }
    }
}
